<template>
  <div class="tags-container">
    <!-- 卡片 -->
    <el-card>
      <!-- 面包屑 -->
      <div slot="header" v-if="subject.id && subject.name">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{ subject.name }}</el-breadcrumb-item>
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-row>
        <!-- 左边 -->
        <el-col :span="18">
          <el-form label-width="80px" size="small" :inline="true">
            <el-form-item label="标签名称">
              <el-input
                v-model="tagList.tagName"
                placeholder="标签名称"
              ></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="tagList.state" placeholder="状态">
                <el-option :value="1" label="启用"></el-option>
                <el-option :value="0" label="禁用"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-form-item style="text-align: right">
                <el-button @click="clear()">清除</el-button>
                <el-button @click="search()" type="primary">搜索</el-button>
              </el-form-item>
            </el-form-item>
          </el-form>
        </el-col>
        <!-- 右边 -->
        <el-col :span="6" style="text-align: right">
          <el-button
            v-if="subject.id && subject.name"
            type="text"
            icon="el-icon-back"
            @click="$router.back()"
            >返回学科</el-button
          >
          <el-button
            icon="el-icon-edit"
            @click="openDialog()"
            type="success"
            size="small"
            >新增标签</el-button
          >
        </el-col>
        <!-- 记录 -->
        <!-- 数据记录 -->
        <el-alert
          :title="`数据一共 ${total} 条`"
          style="margin-bottom: 15px"
          type="info"
          class="alert"
          :closable="false"
          show-icon
        ></el-alert>
        <!-- 表格 -->
        <el-table :data="tags">
          <el-table-column
            label="序号"
            type="index"
            width="80px"
          ></el-table-column>
          <el-table-column
            label="所属学科"
            prop="subjectName"
          ></el-table-column>
          <el-table-column label="标签名称" prop="tagName"></el-table-column>
          <el-table-column label="创建者" prop="username"></el-table-column>
          <el-table-column label="创建日期">
            <template #default="{ row }">{{
              row.addDate | parseTimeByString
            }}</template>
          </el-table-column>
          <el-table-column label="状态">
            <template #default="{ row }">{{
              row.state === 1 ? '已启用' : '已禁用'
            }}</template>
          </el-table-column>
          <el-table-column label="操作" width="150px">
            <template #default="{ row }">
              <el-button type="text" @click="toggleState(row)">{{
                row.state === 1 ? '禁用' : '启用'
              }}</el-button>
              <el-button
                type="text"
                @click="openDialog(row)"
                :disabled="row.state === 1"
                >修改</el-button
              >
              <el-button type="text" @click="del(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          style="margin-top: 20px; text-align: right"
          background
          layout="prev, pager, next, sizes, jumper"
          :total="total"
          :page-size="tagList.pagesize"
          :current-page="tagList.page"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 20, 50]"
          @size-change="handleSizeChange"
          hide-on-single-page
        ></el-pagination>
      </el-row>
    </el-card>
    <!-- 添加|修改 -->
    <tags-add
      ref="tagsAdd"
      :data="currTag"
      @updateList="updateList()"
    ></tags-add>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/tags'
import TagsAdd from '../components/tags-add'
export default {
  name: 'TagsList',
  components: { TagsAdd },
  data () {
    return {
      tagList: {
        tagName: null,
        state: null,
        page: 1,
        pagesize: 5
      },
      total: 0,
      tags: [],
      currTag: {}
    }
  },
  computed: {
    subject () {
      return this.$route.query || {}
    }
  },
  methods: {
    openDialog (tag = {}) {
      this.currTag = tag
      this.$nextTick(() => {
        this.$refs.tagsAdd.open()
      })
    },
    // 新增|修改 后更新列表
    updateList () {
      if (!this.currTag.id) {
        this.tagList.page = 1
      }
      this.getList()
    },
    // 每页显示的数据条数发生了变化
    handleSizeChange (val) {
      this.tagList.page = 1
      this.tagList.pagesize = val
      this.getList()
    },
    // 页码值发生了变化
    handleCurrentChange (val) {
      this.tagList.page = val
      this.getList()
    },
    // 进行筛选
    search () {
      this.tagList.page = 1
      this.getList()
    },
    // 清除
    clear () {
      this.tagList = {
        tagsName: null,
        state: null
      }
    },
    // 获取数据
    async getList () {
      const { data } = await list(this.tagList)
      console.log(data)
      this.tags = data.items
      this.total = data.counts
    },
    // 删除数据
    async del (row) {
      await this.$confirm('此操作将永久删除该标签, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await remove(row)
      this.$message.success('删除成功')
      this.getList()
    },
    // 修改数据
    async toggleState (row) {
      await changeState({
        id: row.id,
        state: row.state === 1 ? 0 : 1
      })
      this.$message.success('操作成功')
      row.state = row.state === 1 ? 0 : 1
    }
  },
  async created () {
    this.getList()
  }
}
</script>

<style lang="scss" scoped>
.tags-container {
  padding: 10px;
}
</style>
